//
//  ResetPassViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/16.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "ResetPassViewController.h"
#import "LoginViewController.h"
@interface ResetPassViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *authCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *mob_Tf;
@property(nonatomic,strong)NSString *mobStr;
@end

@implementation ResetPassViewController
- (IBAction)getValid:(UIButton *)sender {
    [self.view endEditing:1];
    if(self.isfind==1){
    if([self.mob_Tf.text isEqualToString:@""]){
        [WToast showWithText:@"请输入正确的手机号"];
        return;
    }else{
        self.mobStr = self.mob_Tf.text;
    }
    }
    NSString *md5Str = [NSString stringWithFormat:@"km%@bz",self.mobStr];
    NSString *tokens = [XTool md5:md5Str];
    NSDictionary *param = @{@"ctrl":@"common",@"mobile":self.mobStr,@"captcha":tokens,@"ac":@"code"};
    [self weqPostHandle:param success:^(id response) {
       if(response){
            [WToast showWithText:@"发送成功"];
            [self openCountdown];
        }
    } failure:^(NSError *error) {
        
    }];
}

//重置提交
- (IBAction)reset:(id)sender {
    [self.view endEditing:1];
    NSDictionary *param = @{@"ac":@"auth",@"op":@"login",@"ctrl":@"live",@"mobile":self.mobStr,@"password":self.pass.text,@"captcha":self.valid_Tf.text,@"ta":@"re_password"};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            [WToast showWithText:@"更改成功，请重新登录"];
            [XTool SaveDefaultInfo:nil Key:USER_INFO];
            [self.navigationController popViewControllerAnimated:1];
//            LoginViewController *vc = [[LoginViewController alloc]init];
//            [self pushSecondVC:vc];
        }
    } failure:^(NSError *error) {
        
    }];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.mob_Tf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.mob_Tf.text.length >= 11) {
            self.mob_Tf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    return YES;
}
-(void)openCountdown{
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [self.authCodeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                //  [self.authCodeBtn setTitleColor:[UIColor colorFromHexCode:@"FB8557"] forState:UIControlStateNormal];
                self.authCodeBtn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [self.authCodeBtn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                //  [self.authCodeBtn setTitleColor:[UIColor colorFromHexCode:@"979797"] forState:UIControlStateNormal];
                self.authCodeBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigation:1 andTitle:@"忘记密码" andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    self.mob_Tf.delegate = self;
    if(self.isfind==1){
        
    }else{
        self.mob_Tf.hidden = 1;
    }
    NSDictionary *userDic = [XTool GetDefaultInfo:USER_INFO];
    self.mobStr = userDic[@"mobile"];
    self.mob.text = [XTool setMobileToCharters:userDic[@"mobile"]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKb)];
    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view from its nib.
}
-(void)hideKb{
    [self.view endEditing:1];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
